﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace AdatbazisProjekt
{
    public partial class Kartya : Form
    {
        private List<ListViewItem> bevasarloLista = new List<ListViewItem>();
        public buyerStruct s;
        public string nrordvalue = "";
        TermekKeresesForm parent;
        DataTermekKereses TermekAdatok2;
        bool Err;

        public Kartya(List<ListViewItem> bl, TermekKeresesForm parent)
        {
            InitializeComponent();
            this.bevasarloLista = bl;
            listaFeltolt();
            listaRichText.ReadOnly = true;
            s = new buyerStruct();
            this.parent = parent;
            TermekAdatok2 = new DataTermekKereses(ref Err);
            if (Err)
            {
                MessageBox.Show("Hiba a Form inicializalasa kozben");
            }
        }

        private void MegseButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void listaFeltolt()
        {
            for (int i = 0; i < bevasarloLista.Count; i++)
            {
                listaRichText.AppendText(bevasarloLista[i].SubItems[0].Text + System.Environment.NewLine);
            }
        }

        private void searchVasarCB_SelectedIndexChanged(object sender, EventArgs e)
        {
            VasarloNevTextBox2.Text = s.nev[searchVasarCB.SelectedIndex];
            VasarloLakhelyTextBox2.Text = s.cim[searchVasarCB.SelectedIndex];
            VasarloSzemSzamTextBox2.Text = s.szemszam[searchVasarCB.SelectedIndex];
            RegComTextBox2.Text = s.regcom[searchVasarCB.SelectedIndex];
            BankTextBox2.Text = s.bank[searchVasarCB.SelectedIndex];
            BankSzamlaTextBox2.Text = s.bankszam[searchVasarCB.SelectedIndex];
            VasarloTelSzamTextBox2.Text = s.telo[searchVasarCB.SelectedIndex];
            Random random = new Random();
            CodFiscTextBox2.Text = (1000 + random.Next(9000)).ToString();

            NrOrdTextBox2.Text = nrordvalue;
        }

        private void searchVasarCB_TextUpdate(object sender, EventArgs e)
        {
            //ne tunj el
            UserDal p = new UserDal();
            s = p.getBuyerSimilar(searchVasarCB.Text);
            searchVasarCB.DataSource = s.nev;
            nrordvalue = p.maxord();
        }

        private void szamlaButton_Click(object sender, EventArgs e)
        {
            string datumMost = DateTime.Now.ToString("d/M/yyyy"); ;

            String nevek = "";
            int ar = 0;

            for (int i = 0; i < bevasarloLista.Count; i++)
            {
                nevek = nevek + bevasarloLista[i].SubItems[0].Text + System.Environment.NewLine;
                ar = ar + Convert.ToInt32(bevasarloLista[i].SubItems[3].Text);
            }
            Utils.szamlaPrint(NrOrdTextBox2.Text, CodFiscTextBox2.Text, nevek,
                ar.ToString(), VasarloNevTextBox2.Text, VasarloSzemSzamTextBox2.Text,
                VasarloLakhelyTextBox2.Text, BankTextBox2.Text, BankSzamlaTextBox2.Text,
                RegComTextBox2.Text, datumMost);
            
        }

        private void kifizetButton_Click(object sender, EventArgs e)
        {
            // Amennyiben eladjuk a termeket meghivjuk az adatbazis modosito metodust es kilepunk a formra vissza a termekekhez
            List<string> darab = new List<string>();
            bool stop = false;
            for (int i = 0; i < bevasarloLista.Count && !stop; i++)
            {
                darab.Add(bevasarloLista[i].SubItems[5].Text);
                TermekAdatok2.TermekEladas(bevasarloLista[i].SubItems[0].Text, bevasarloLista[i].SubItems[3].Text, VasarloNevTextBox2.Text, VasarloSzemSzamTextBox2.Text, VasarloLakhelyTextBox2.Text, VasarloTelSzamTextBox2.Text, RegComTextBox2.Text, BankTextBox2.Text, BankSzamlaTextBox2.Text, CodFiscTextBox2.Text, NrOrdTextBox2.Text, ref Err);
                if (Err)
                {
                    for (int j = 0; j < darab.Count; j++)
                    {
                        TermekAdatok2.TermekAdatFrissit(bevasarloLista[j].SubItems[0].Text, bevasarloLista[j].SubItems[1].Text, bevasarloLista[j].SubItems[2].Text, bevasarloLista[j].SubItems[4].Text, bevasarloLista[j].SubItems[3].Text, bevasarloLista[j].SubItems[0].Text, bevasarloLista[j].SubItems[1].Text, bevasarloLista[j].SubItems[2].Text, bevasarloLista[j].SubItems[3].Text, bevasarloLista[j].SubItems[4].Text, darab[j], ref Err); 
                    }
                    stop = true;
                }
            }

            if (!stop)
            {
                MessageBox.Show("A termek(ek) sikeresen eladva!");
            }
            else
            {
                MessageBox.Show("Az eladas sajnos sikertelen volt!");
            }
            
            parent.feltolt(TermekAdatok2.GetTermekInf());
            this.Close();
        }





    }
}
